﻿using System;
using BGShell.PowerShell.Host;

namespace BGShell.PowerShell.Runspaces
{
    internal static class RunspaceProxyFactory
    {
        private static T CreateInstance<T>(AppDomain domain, params object[] args) where T : MarshalByRefObject
        {
            return (T)domain.CreateInstanceAndUnwrap(
                typeof(T).Assembly.FullName,
                typeof(T).FullName,
                false,
                0, null,
                args,
                null,
                null,
                AppDomain.CurrentDomain.Evidence);
        }

        public static IRunspaceProxy CreateRunspace(AppDomain domain, RunspaceHostInfo hostInfo, IRunspaceHostUI hostUi, MarshalByRefObject hostPrivateObject)
        {
            if (domain == AppDomain.CurrentDomain)
            {
                return new RunspaceProxyService(hostInfo, hostUi, hostPrivateObject);
            }

            return CreateInstance<RunspaceProxyService>(domain, hostInfo, hostUi, hostPrivateObject);
        }

    }
}
